package org.linlinjava.litemall.wx.service;

import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallRentOrderDetailService;
import org.linlinjava.litemall.db.service.LitemallRentOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class WxRentOrderDetailService {
    @Autowired
    private LitemallRentOrderService rentOrderService;
    @Autowired
    private LitemallGoodsProductService goodsProductService;
    @Autowired
    private LitemallRentOrderDetailService rentOrderDetailService;
    @Autowired
    private LitemallGoodsService goodsService;

    /**
     * 查询我借过的书
     *
     * @param userId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<Map<String,Object>> queryMyBorrowedBook(Integer userId, Integer page, Integer limit, String sort, String order) {
        List<LitemallRentOrder> rentOrders = rentOrderService.queryByUIdAndStatusNe(userId);
        if (rentOrders == null || rentOrders.size() == 0) {
            return new ArrayList<>();
        }
        List<LitemallRentOrderDetail> rentOrderDetails = new ArrayList<>();
        rentOrders.forEach(rentOrder -> rentOrderDetails.addAll(rentOrderDetailService.queryByOid(rentOrder.getId())));

        List<Integer> goodsProductIds = new ArrayList<>();
        rentOrderDetails.forEach(rentOrderDetail -> goodsProductIds.add(rentOrderDetail.getGoodsProductId()));

        List<LitemallGoodsProduct> goodsProductList = goodsProductService.queryIdIn(goodsProductIds, page, limit, sort, order);
        List<Map<String,Object>> listMap = new ArrayList<>();
        goodsProductList.forEach(goodsProduct->{
            Map<String,Object> map = new HashMap<>();
            map.put("goods",goodsService.findById(goodsProduct.getGoodsId()));
            map.put("goodsProductIds",goodsProduct.getId());
            map.put("goodsProduct",goodsProduct);
            listMap.add(map);
        });
        return listMap;
    }
}
